package com.yf1706.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yf1706.bean.SocialSecurity;
import com.yf1706.bean.SocialSecuritySob;
import com.yf1706.bean.SocialSecurityType;
import com.yf1706.service.SocialSecurityService;
import com.yf1706.tools.JsonTool;
import com.yf1706.vo.EmpSSVO;
import com.yf1706.vo.SocialSecurityVO;

@Controller
@RequestMapping("socialSecurity")
public class SocialSecurityController extends BaseController {

	@Autowired
	private SocialSecurityService securityService;

	// --------------社保账套-------------------
	@RequestMapping("insertSob")
	@ResponseBody
	public void insertSob(HttpServletRequest request, HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_SSsob_rowData"));
		SocialSecuritySob securitySob = new SocialSecuritySob();
		securitySob.setSocialSecuritySobName(jObject.getString("socialSecuritySobName"));
		// securitySob.setBuilder(getSessionUserName(request));
		securitySob.setBuildDate(new Date());
		securitySob.setRemark(jObject.getString("remark"));
		JsonTool jTool = new JsonTool();
		try {
			securityService.saveSob(securitySob);
			jTool.setSuccess(true);
			jTool.setMsg("添加社保账套【" + securitySob.getSocialSecuritySobName() + "】成功！");
		} catch (Exception e) {
			jTool.setMsg("添加社保账套【" + securitySob.getSocialSecuritySobName() + "】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("deleteSob")
	@ResponseBody
	public void deleteSob(HttpServletRequest request, HttpServletResponse response) {
		String ids = removeFirstAndLastChar(request.getParameter("djt_SSsob_rowData"));
		JsonTool jTool = new JsonTool();
		try {
			// securityService.removeSob(ids);
			jTool.setSuccess(true);
			jTool.setMsg("删除社保账套【成功】！");
		} catch (Exception e) {
			jTool.setMsg("删除社保账套【失败】！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("updateSob")
	@ResponseBody
	public void updateSob(HttpServletRequest request, HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_SSsob_rowData"));
		SocialSecuritySob securitySob = new SocialSecuritySob();
		securitySob.setSocialSecuritySobName(jObject.getString("socialSecuritySobName"));
		securitySob.setRemark(jObject.getString("remark"));
		securitySob.setEmpNum(jObject.getIntValue("empNum"));
		securitySob.setSocialSecuritySobId(jObject.getIntValue("socialSecuritySobId"));
		JsonTool jTool = new JsonTool();
		try {
			securityService.updateSob(securitySob);
			jTool.setSuccess(true);
			jTool.setMsg("编辑社保账套【" + securitySob.getSocialSecuritySobName() + "】成功！");
		} catch (Exception e) {
			jTool.setMsg("编辑社保账套【" + securitySob.getSocialSecuritySobName() + "】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("querySobByPage")
	@ResponseBody
	public Map<String, Object> querySobByPage(int page, int rows, String sort, String order) {
		Page<SocialSecuritySob> pages = securityService.querySobByPage(page, rows, sort, order);
		Long total = pages.getTotalElements();
		List<SocialSecuritySob> list = pages.getContent();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	@RequestMapping("findSobByPage")
	@ResponseBody
	public Map<String, Object> findSobByPage(String socialSecuritySobName,int page,int rows,String sort,String order) {
		Page<SocialSecuritySob> pages = securityService.findSearch(socialSecuritySobName, page, rows, sort, order);
		Long total= pages.getTotalElements();
		List<SocialSecuritySob> list = pages.getContent();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}
	
	// --------------社保表---------------------
	@RequestMapping("insertSS")
	@ResponseBody
	public void insertSS(HttpServletRequest request, HttpServletResponse response) {
		SocialSecurity socialSecurity = new SocialSecurity();
		JsonTool jTool = new JsonTool();
		try {
			int empId = Integer.valueOf(request.getParameter("empName"));
			socialSecurity.setEmp(securityService.findEmpByID(empId));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String SendDate = request.getParameter("endDate");
			if (!(SendDate.equals(""))) {
				Date endDate = sdf.parse(request.getParameter("endDate"));
				socialSecurity.setEndDate(endDate);
			}
			Date beginDate = sdf.parse(request.getParameter("beginDate"));
			socialSecurity.setBeginDate(beginDate);
			String remark = request.getParameter("remark");
			socialSecurity.setRemark(remark);
			int socialSecuritySobId = Integer.valueOf(request.getParameter("socialSecuritySobId"));
			socialSecurity.setSocialSecuritySob(securityService.findSobByID(socialSecuritySobId));
			// -----------------计算公司应缴纳社保总额和个人应缴纳社保总额--------------------
			// 社保基数
			int socialSecurityBaseNumber = Integer.valueOf(request.getParameter("socialSecurityBaseNumber"));
			socialSecurity.setSocialSecurityBaseNumber(socialSecurityBaseNumber);
			// 公积金基数
			int publicReserveFunds = Integer.valueOf(request.getParameter("publicReserveFunds"));
			socialSecurity.setPublicReserveFunds(publicReserveFunds);
			// 社保类型ID
			int socialSecurityTypeId = Integer.valueOf(request.getParameter("socialSecurityTypeId"));
			SocialSecurityType securityType = securityService.findTypeByID(socialSecurityTypeId);
			socialSecurity.setSocialSecurityType(securityType);
			Double socialCompanyValue = securityService.countCocialCompanyValue(securityType, publicReserveFunds,
					socialSecurityBaseNumber);
			socialSecurity.setSocialSecurityCompanyValue(socialCompanyValue);
			Double socialPersonValue = securityService.countsocialPersonValue(securityType, publicReserveFunds,
					socialSecurityBaseNumber);
			socialSecurity.setSocialSecurityPersonValue(socialPersonValue);
			securityService.saveSS(socialSecurity);
			jTool.setSuccess(true);
			jTool.setMsg("添加社保信息成功！");
		} catch (Exception e) {
			jTool.setMsg("添加社保信息失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("deleteSS")
	@ResponseBody
	public void deleteSS(HttpServletRequest request, HttpServletResponse response) {
		String ids = removeFirstAndLastChar(request.getParameter("djt_SS_rowData"));
		JsonTool jTool = new JsonTool();
		try {
			securityService.removeSS(ids);
			jTool.setSuccess(true);
			jTool.setMsg("删除员工社保信息【成功】！");
		} catch (Exception e) {
			jTool.setMsg("删除员工社保信息【失败】！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("updateSS")
	@ResponseBody
	public void updateSS(HttpServletRequest request, HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_SS_rowData"));
		SocialSecurity socialSecurity = new SocialSecurity();
		socialSecurity.setSocialSecuritySob(securityService.findSobByID(jObject.getIntValue("socialSecuritySobId")));
		socialSecurity.setRemark(jObject.getString("remark"));
		socialSecurity.setSocialSecurityId(jObject.getIntValue("socialSecurityId"));
		socialSecurity.setBeginDate(jObject.getDate("beginDate"));
		socialSecurity.setEndDate(jObject.getDate("endDate"));
		// -----------------计算公司应缴纳社保总额和个人应缴纳社保总额--------------------
		SocialSecurityType securityType = securityService.findTypeByID(jObject.getIntValue("socialSecurityTypeId"));
		socialSecurity.setSocialSecurityType(securityType);
		int socialSecurityBaseNumber = jObject.getIntValue("socialSecurityBaseNumber");
		socialSecurity.setSocialSecurityBaseNumber(socialSecurityBaseNumber);
		int publicReserveFunds = jObject.getIntValue("publicReserveFunds");
		socialSecurity.setPublicReserveFunds(publicReserveFunds);
		Double socialCompanyValue = securityService.countCocialCompanyValue(securityType, publicReserveFunds,
				socialSecurityBaseNumber);
		socialSecurity.setSocialSecurityCompanyValue(socialCompanyValue);
		Double socialPersonValue = securityService.countsocialPersonValue(securityType, publicReserveFunds,
				socialSecurityBaseNumber);
		socialSecurity.setSocialSecurityPersonValue(socialPersonValue);
		JsonTool jTool = new JsonTool();
		try {
			securityService.updateSS(socialSecurity);
			jTool.setSuccess(true);
			jTool.setMsg("编辑【" + jObject.getString("empName") + "】的社保信息成功！");
		} catch (Exception e) {
			jTool.setMsg("编辑【" + jObject.getString("empName") + "】的社保信息失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("findSSByPage")
	@ResponseBody
	public Map<String, Object> findSSByPage(int id, int page, int rows, String sort, String order) {
		Map<String, Object> map = new HashMap<String, Object>();
		Page<SocialSecurityVO> pages = securityService.findSSByPage(id, page, rows, sort, order);
		Long total = pages.getTotalElements();
		List<SocialSecurityVO> list = pages.getContent();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	/*@RequestMapping("findBySAndD")
	@ResponseBody
	public List<SocialSecurityVO> findSearchBySidAndDate(int sId,String countDate) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date endDate = sdf.parse(countDate);
		return securityService.findSearchBySidAndDate(sId, endDate);
	}*/
	
	// --------------社保类型-------------------
	@RequestMapping("insertType")
	@ResponseBody
	public void insertType(HttpServletRequest request, HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_SSType_rowData"));
		SocialSecurityType securityType = new SocialSecurityType();
		securityType.setCompanyAccumulationFundRate(jObject.getDoubleValue("companyAccumulationFundRate"));
		securityType.setCompanyChildbirthInsuranceRate(jObject.getDoubleValue("companyChildbirthInsuranceRate"));
		securityType.setCompanyEmploymentInjuryInsuranceRate(
				jObject.getDoubleValue("companyEmploymentInjuryInsuranceRate"));
		securityType.setCompanyEndowmentInsuranceRate(jObject.getDoubleValue("companyEndowmentInsuranceRate"));
		securityType.setCompanyMedicalInsuranceRate(jObject.getDoubleValue("companyMedicalInsuranceRate"));
		securityType.setCompanyUnemploymentInsuranceRate(jObject.getDoubleValue("companyUnemploymentInsuranceRate"));
		securityType.setPersonAccumulationFundRate(jObject.getDoubleValue("personAccumulationFundRate"));
		securityType.setPersonChildbirthInsuranceRate(jObject.getDoubleValue("personChildbirthInsuranceRate"));
		securityType
				.setPersonEmploymentInjuryInsuranceRate(jObject.getDoubleValue("personEmploymentInjuryInsuranceRate"));
		securityType.setPersonEndowmentInsuranceRate(jObject.getDoubleValue("personEndowmentInsuranceRate"));
		securityType.setPersonMedicalInsuranceRate(jObject.getDoubleValue("personMedicalInsuranceRate"));
		securityType.setPersonUnemploymentInsuranceRate(jObject.getDoubleValue("personUnemploymentInsuranceRate"));
		securityType.setSocialSecurityTypeName(jObject.getString("socialSecurityTypeName"));
		JsonTool jTool = new JsonTool();
		try {
			securityService.saveType(securityType);
			jTool.setSuccess(true);
			jTool.setMsg("添加社保类型：【" + securityType.getSocialSecurityTypeName() + "】成功！");
		} catch (Exception e) {
			jTool.setMsg("添加社保类型：【" + securityType.getSocialSecurityTypeName() + "】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("deleteType")
	@ResponseBody
	public void deleteType(HttpServletRequest request, HttpServletResponse response) {
		String ids = removeFirstAndLastChar(request.getParameter("djt_SSType_rowData"));
		JsonTool jTool = new JsonTool();
		try {
			securityService.removeType(ids);
			jTool.setSuccess(true);
			jTool.setMsg("删除社保类型【成功】！");
		} catch (Exception e) {
			jTool.setMsg("删除社保类型【失败】！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("updateType")
	@ResponseBody
	public void updateType(HttpServletRequest request, HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_SSType_rowData"));
		SocialSecurityType securityType = new SocialSecurityType();
		securityType.setCompanyAccumulationFundRate(jObject.getDoubleValue("companyAccumulationFundRate"));
		securityType.setCompanyChildbirthInsuranceRate(jObject.getDoubleValue("companyChildbirthInsuranceRate"));
		securityType.setCompanyEmploymentInjuryInsuranceRate(
				jObject.getDoubleValue("companyEmploymentInjuryInsuranceRate"));
		securityType.setCompanyEndowmentInsuranceRate(jObject.getDoubleValue("companyEndowmentInsuranceRate"));
		securityType.setCompanyMedicalInsuranceRate(jObject.getDoubleValue("companyMedicalInsuranceRate"));
		securityType.setCompanyUnemploymentInsuranceRate(jObject.getDoubleValue("companyUnemploymentInsuranceRate"));
		securityType.setPersonAccumulationFundRate(jObject.getDoubleValue("personAccumulationFundRate"));
		securityType.setPersonChildbirthInsuranceRate(jObject.getDoubleValue("personChildbirthInsuranceRate"));
		securityType
				.setPersonEmploymentInjuryInsuranceRate(jObject.getDoubleValue("personEmploymentInjuryInsuranceRate"));
		securityType.setPersonEndowmentInsuranceRate(jObject.getDoubleValue("personEndowmentInsuranceRate"));
		securityType.setPersonMedicalInsuranceRate(jObject.getDoubleValue("personMedicalInsuranceRate"));
		securityType.setPersonUnemploymentInsuranceRate(jObject.getDoubleValue("personUnemploymentInsuranceRate"));
		securityType.setSocialSecurityTypeName(jObject.getString("socialSecurityTypeName"));
		securityType.setSocialSecurityTypeId(jObject.getIntValue("socialSecurityTypeId"));
		JsonTool jTool = new JsonTool();
		try {
			securityService.updateType(securityType);
			jTool.setSuccess(true);
			jTool.setMsg("编辑社保类型：【" + securityType.getSocialSecurityTypeName() + "】成功！");
		} catch (Exception e) {
			jTool.setMsg("编辑社保类型：【" + securityType.getSocialSecurityTypeName() + "】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("queryTypeByPage")
	@ResponseBody
	public Map<String, Object> queryTypeByPage(int page, int rows) {
		Page<SocialSecurityType> pages = securityService.queryTypeByPage(page, rows);
		Long total = pages.getTotalElements();
		List<SocialSecurityType> list = pages.getContent();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	@RequestMapping("queryTypeAll")
	@ResponseBody
	public List<SocialSecurityType> queryTypeAll() {
		return securityService.queryTypeAll();
	}

	// ---------------员工-----------------
	@RequestMapping("findEmpByDepID")
	@ResponseBody
	public List<EmpSSVO> findEmpByDepID(int did) {
		return securityService.findEmpByDepID(did);
	}
}
